data "alicloud_zones" "slb" {
  available_resource_creation = "Slb"
  available_slb_address_type  = "classic_intranet"
}

resource "alicloud_slb_load_balancer" "instance" {
  load_balancer_name = "ebike"
  vswitch_id         = var.exist_vswitch_ids[index(var.exist_azs, data.alicloud_zones.slb.zones[0].id)]
  address_type       = "intranet"
  load_balancer_spec = "slb.s2.small"
}

resource "alicloud_slb_backend_server" "default" {
  load_balancer_id = alicloud_slb_load_balancer.instance.id

  backend_servers {
    server_id = var.instance_ids[0]
    weight    = 100
  }

  backend_servers {
    server_id = var.instance_ids[1]
    weight    = 100
  }
  depends_on = [alicloud_slb_load_balancer.instance]
}

locals {
  idx = range(1, 100)

  virtual_server_group = [for i in var.virtual_server_group:
    {
      name: lookup(i, "name")
      virtual_server: [
        for v in lookup(i, "virtual_server"):
          {server_ids = element(var.instance_ids, index(local.idx, v.server_ids)), port = v.port}
      ]
    }
  ]
}

resource "alicloud_slb_server_group" "main" {
  count = length(local.virtual_server_group)
  load_balancer_id = alicloud_slb_load_balancer.instance.id
  name             = lookup(element(local.virtual_server_group, count.index), "name")

  dynamic "servers" {
    for_each       = lookup(element(local.virtual_server_group, count.index), "virtual_server")
    content {
      server_ids   = [lookup(servers.value, "server_ids")]
      port         = lookup(servers.value, "port")
      weight       = lookup(servers.value, "weight", "100")
      type         = lookup(servers.value, "type", "ecs")
    }
  }
  depends_on = [alicloud_slb_load_balancer.instance]
}

resource "alicloud_slb_listener" "http_80" {
  load_balancer_id = alicloud_slb_load_balancer.instance.id
  frontend_port    = 80
  protocol         = "http"
  listener_forward = "on"
  forward_port     = alicloud_slb_listener.https_443.frontend_port
  depends_on = [
    alicloud_slb_load_balancer.instance,
    alicloud_slb_listener.https_443
  ]
}

resource "alicloud_slb_listener" "https_443" {
  load_balancer_id          = alicloud_slb_load_balancer.instance.id
  frontend_port             = 443
  backend_port              = 80
  protocol                  = "https"
  sticky_session            = "off"
  health_check              = "on"
  health_check_type         = "http"
  health_check_uri          = "/health"
  health_check_connect_port = 20
  healthy_threshold         = 8
  unhealthy_threshold       = 8
  health_check_timeout      = 8
  health_check_interval     = 5
  health_check_http_code    = "http_2xx,http_3xx"
  bandwidth                 = 10
  server_certificate_id     = alicloud_slb_server_certificate.main.id
  server_group_id           = alicloud_slb_server_group.main[0].id
  depends_on = [alicloud_slb_load_balancer.instance]
}

resource "alicloud_slb_server_certificate" "main" {
  name               = "ebike"
  server_certificate = "-----BEGIN CERTIFICATE-----\nMIIDRjCCAq+gAwIBAgIJAJn3ox4K13PoMA0GCSqGSIb3DQEBBQUAMHYxCzAJBgNV\nBAYTAkNOMQswCQYDVQQIEwJCSjELMAkGA1UEBxMCQkoxDDAKBgNVBAoTA0FMSTEP\nMA0GA1UECxMGQUxJWVVOMQ0wCwYDVQQDEwR0ZXN0MR8wHQYJKoZIhvcNAQkBFhB0\nZXN0QGhvdG1haWwuY29tMB4XDTE0MTEyNDA2MDQyNVoXDTI0MTEyMTA2MDQyNVow\ndjELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAkJKMQswCQYDVQQHEwJCSjEMMAoGA1UE\nChMDQUxJMQ8wDQYDVQQLEwZBTElZVU4xDTALBgNVBAMTBHRlc3QxHzAdBgkqhkiG\n9w0BCQEWEHRlc3RAaG90bWFpbC5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ\nAoGBAM7SS3e9+Nj0HKAsRuIDNSsS3UK6b+62YQb2uuhKrp1HMrOx61WSDR2qkAnB\ncoG00Uz38EE+9DLYNUVQBK7aSgLP5M1Ak4wr4GqGyCgjejzzh3DshUzLCCy2rook\nKOyRTlPX+Q5l7rE1fcSNzgepcae5i2sE1XXXzLRIDIvQxcspAgMBAAGjgdswgdgw\nHQYDVR0OBBYEFBdy+OuMsvbkV7R14f0OyoLoh2z4MIGoBgNVHSMEgaAwgZ2AFBdy\n+OuMsvbkV7R14f0OyoLoh2z4oXqkeDB2MQswCQYDVQQGEwJDTjELMAkGA1UECBMC\nQkoxCzAJBgNVBAcTAkJKMQwwCgY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cbpWdDXa7m8J3wRr3o5nUB+TPQ5nzAbthM\nzWX931YQeACcwhxvHQJBAN5mTzzJD4w4Ma6YTaNHyXakdYfyAWrOkPIWZxfhMfXe\nDrlNdiysTI4Dd1dLeErVpjsckAaOW/JDG5PCSwkaMxk=\n-----END RSA PRIVATE KEY-----"
}

resource "alicloud_slb_listener" "tcp_9880" {
  load_balancer_id          = alicloud_slb_load_balancer.instance.id
  frontend_port             = 9880
  protocol                  = "tcp"
  bandwidth                 = -1

  health_check              = "on"
  health_check_type         = "tcp"
  health_check_interval     = 2
  health_check_uri          = "/"
  health_check_timeout      = 5
  healthy_threshold         = 3
  unhealthy_threshold       = 3
  scheduler                 = "wrr"
  established_timeout       = 900
  server_group_id           = alicloud_slb_server_group.main[4].id

  depends_on = [alicloud_slb_load_balancer.instance]
}

resource "alicloud_slb_rule" "ebike" {
  load_balancer_id          = alicloud_slb_load_balancer.instance.id
  frontend_port             = alicloud_slb_listener.https_443.frontend_port
  name                      = "ebike"
  url                       = "/ebike/v2/backend"
  server_group_id           = alicloud_slb_server_group.main[0].id
  cookie                    = "23ffsa"
  cookie_timeout            = 100
  health_check_http_code    = "http_2xx"
  health_check_interval     = 10
  health_check_uri          = "/health"
  health_check_connect_port = 80
  health_check_timeout      = 30
  healthy_threshold         = 4
  unhealthy_threshold       = 4
  sticky_session            = "on"
  sticky_session_type       = "server"
  listener_sync             = "off"
  scheduler                 = "rr"
  health_check_domain       = "test"
  health_check              = "on"
  depends_on = [
    alicloud_slb_load_balancer.instance,
    alicloud_slb_listener.https_443,
    alicloud_slb_server_group.main
  ]
}

resource "alicloud_slb_rule" "ebike_high" {
  load_balancer_id          = alicloud_slb_load_balancer.instance.id
  frontend_port             = alicloud_slb_listener.https_443.frontend_port
  name                      = "ebike_high"
  url                       = "/ebike/v3/backend"
  server_group_id           = alicloud_slb_server_group.main[1].id
  cookie                    = "23ffsa"
  cookie_timeout            = 100
  health_check_http_code    = "http_2xx"
  health_check_interval     = 10
  health_check_uri          = "/health"
  health_check_connect_port = 80
  health_check_timeout      = 30
  healthy_threshold         = 4
  unhealthy_threshold       = 4
  sticky_session            = "on"
  sticky_session_type       = "server"
  listener_sync             = "off"
  scheduler                 = "rr"
  health_check_domain       = "test"
  health_check              = "on"
  depends_on = [
    alicloud_slb_load_balancer.instance,
    alicloud_slb_listener.https_443,
    alicloud_slb_server_group.main,
    alicloud_slb_rule.ebike
  ]
}

resource "alicloud_slb_rule" "worker" {
  load_balancer_id          = alicloud_slb_load_balancer.instance.id
  frontend_port             = alicloud_slb_listener.https_443.frontend_port
  name                      = "worker"
  url                       = "/worker"
  server_group_id           = alicloud_slb_server_group.main[2].id
  cookie                    = "23ffsa"
  cookie_timeout            = 100
  health_check_http_code    = "http_2xx"
  health_check_interval     = 10
  health_check_uri          = "/health"
  health_check_connect_port = 80
  health_check_timeout      = 30
  healthy_threshold         = 4
  unhealthy_threshold       = 4
  sticky_session            = "on"
  sticky_session_type       = "server"
  listener_sync             = "off"
  scheduler                 = "rr"
  health_check_domain       = "test"
  health_check              = "on"
  depends_on = [
    alicloud_slb_load_balancer.instance,
    alicloud_slb_listener.https_443,
    alicloud_slb_server_group.main,
    alicloud_slb_rule.ebike_high
  ]
}

resource "alicloud_slb_rule" "mbserver" {
  load_balancer_id          = alicloud_slb_load_balancer.instance.id
  frontend_port             = alicloud_slb_listener.https_443.frontend_port
  name                      = "mbserver"
  url                       = "/mieba"
  server_group_id           = alicloud_slb_server_group.main[3].id
  cookie                    = "23ffsa"
  cookie_timeout            = 100
  health_check_http_code    = "http_2xx"
  health_check_interval     = 10
  health_check_uri          = "/health"
  health_check_connect_port = 80
  health_check_timeout      = 30
  healthy_threshold         = 4
  unhealthy_threshold       = 4
  sticky_session            = "on"
  sticky_session_type       = "server"
  listener_sync             = "off"
  scheduler                 = "rr"
  health_check_domain       = "test"
  health_check              = "on"

  depends_on = [
    alicloud_slb_load_balancer.instance,
    alicloud_slb_listener.https_443,
    alicloud_slb_server_group.main,
    alicloud_slb_rule.worker
  ]
}